#!/bin/sh
# function : rysnc MASTERIP  to  BACKUPIP
log=/var/log/rsync.log
src="/home/nfsdata/"
host="10.64.2.14"
module="syncdir"

pid_count=`ps -ef | grep '/usr/local/bin/inotifywait -mr --timefmt %d/%m/%y %H:%M --format %T %w %f -e close_write,modify,delete,create,attrib /home/nfsdata/' | grep -v 'grep' |wc -l`

rsync_file(){
        /usr/local/bin/inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e close_write,modify,delete,create,attrib $src \
        | while read DATE TIME DIR FILE; do

       FILECHANGE=${DIR}${FILE}

       /usr/bin/rsync -avH --delete  --progress --password-file=/etc/rsync.pas $src  --exclude-from="/etc/inotify/rules.txt" rsyncuser@$host::$module &
       echo "At ${TIME} on ${DATE}, file $FILECHANGE was backed up via rsync" >> $log
          done
}

if (( ${pid_count} < 1 )); then
rsync_file &
/usr/bin/rsync -avH --delete  --progress --password-file=/etc/rsync.pas /home/nfsdata/  --exclude-from="/etc/inotify/rules.txt" rsyncuser@${host}::syncdir &
fi
